package pro.zfb.service.zfb.enter.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoleilu.hutool.json.JSONObject;

import com.yy.kit.log.YYLogKits;
import com.yy.kit.string.YYStrKits;
import com.yy.kit.zfb.constans.AlipayConstants;
import pro.zfb.entity.TZfbFollow;
import pro.zfb.entity.TZfbFollowExample;
import pro.zfb.mapper.TZfbFollowMapper;
import pro.zfb.service.zfb.enter.IZFBEnterService;

@Service(value = "iZFBEnterService")
public class ZFBEnterServiceImpl implements IZFBEnterService {
	
	private final TZfbFollowMapper tZfbFollowMapper;

	@Autowired
	public ZFBEnterServiceImpl(TZfbFollowMapper tZfbFollowMapper) {
		this.tZfbFollowMapper = tZfbFollowMapper;
	}

	@Override
	public String enter(JSONObject biz_content) {
		//1. 获取用户openid
		String FromAlipayUserId = biz_content.getStr(AlipayConstants.FROMUSER);
		JSONObject js = new JSONObject(biz_content.getStr("UserInfo"));
		String user_name = js.getStr("user_name");
		String logon_id = js.getStr("logon_id");
		
		YYLogKits.info(ZFBEnterServiceImpl.class, "├用户openid:{}进入生活号!", FromAlipayUserId);
		
		//2. 查询用户是否已经在关注表中
		TZfbFollowExample example = new TZfbFollowExample();
		example.createCriteria().andOpenidEqualTo(FromAlipayUserId);
		long count = tZfbFollowMapper.countByExample(example);
		
		//3.表中不存在则插入，如果存在，不做处理
		if (count <= 0) {
			TZfbFollow follow = new TZfbFollow();
			follow.setId(YYStrKits.getTableId());
			follow.setOpenid(FromAlipayUserId);
			follow.setFollow_status(1);
			follow.setFollow_count(1);
			follow.setFollow_date(new Date());
			follow.setLogon_id(logon_id);
			follow.setUser_name(user_name);
			tZfbFollowMapper.insert(follow);
		}
		return "";
	}

}
